﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Seagull.BarTender.Print;

namespace 大连顺秋服装厂产线
{
    public partial class Frm包装贴 : Form
    {
        private 包装贴DTO 包装贴DTO;
        public Frm包装贴()
        {
            InitializeComponent();
        }

        private void textBox生产号_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                try
                {
                    DataRow dataRow = SQLmtm.GetDataRow("SELECT\n" +
                        "	o_order_p.SHOP_ID AS shopId,\n" +
                        "   o_order_p.SHOP_NAME AS shopName,\n"+
                        "   o_order_p.ORDER_STATUS_CD,\n"+
                        "	o_order_p.ORDER_NO AS orderNo,\n" +
                        "	i_material_p.MATERIAL_CODE AS materialNo,\n" +
                        "	o_order_p.CUSTOM_NAME AS customerName,\n" +
                        "	RIGHT ( o_order_p.ORDER_NO, 10 ) AS barcodeNo,\n" +
                        "	o_order_p.ORDER_NO AS businessNo \n" +
                        "FROM\n" +
                        "	`o_order_p`\n" +
                        "	LEFT JOIN s_style_p ON o_order_p.STYLE_ID = s_style_p.SYS_STYLE_ID\n" +
                        "	LEFT JOIN i_material_p ON s_style_p.SYTLE_FABRIC_ID = i_material_p.MATERIAL_ID \n" +
                        "WHERE\n" +
                        "	RIGHT ( ORDER_NO, 6 ) = '" + this.textBox生产号.Text.Trim() + "'");
                    if (dataRow["ORDER_STATUS_CD"].ToString().Equals("ORDER_STATUS-OS_80"))
                    {
                        MessageBox.Show("订单状态为取消请手动更改订单状态");
                    }
                    if (dataRow["shopId"].ToString().Equals("46"))
                    {
                        this.包装贴DTO = new 包装贴DTO(
                            this.textBox生产号.Text.Trim(),
                            dataRow["businessNo"].ToString(),
                            dataRow["orderNo"].ToString(),
                            dataRow["customerName"].ToString(),
                            dataRow["materialNo"].ToString(),
                            dataRow["barcodeNo"].ToString(),
                            dataRow["shopName"].ToString());
                    }
                    else
                    {
                        this.包装贴DTO = FunctionHelper.JsonDeserialization<包装贴DTO>(RestCall.httpGetMethod(URL.接口url + "/barcode/getPachageLabelPrintByBarcode?barcode=" + this.textBox生产号.Text.Trim()));
                    }
                    if (String.IsNullOrWhiteSpace(this.包装贴DTO.barcode))
                    {
                        MessageBox.Show("暂无信息");
                        return;
                    }
                }
                catch
                {
                    MessageBox.Show("暂无信息");
                }

                this.print包装贴();
            }
        }

        private void print包装贴()
        {
            //String str = System.AppDomain.CurrentDomain.BaseDirectory;
            Engine btEngine = new Engine();
            btEngine.Start();
            LabelFormatDocument btFormat = btEngine.Documents.Open("C://包装贴.btw");
            //指定打印机名 
            //btFormat.PrintSetup.PrinterName = "HPRT HLP106S-UE";
            //btFormat.PrintSetup.PrinterName = "迅捷PDF虚拟打印机";
            btFormat.PrintSetup.PrinterName = "POSTEK G-3106";
            //打印份数                   
            btFormat.PrintSetup.IdenticalCopiesOfLabel = 2;
            //改变标签打印数份连载 
            btFormat.PrintSetup.NumberOfSerializedLabels = 2;
            //对BTW模版相应字段进行赋值 

            btFormat.SubStrings["businessNo"].Value = this.包装贴DTO.businessNo;
            btFormat.SubStrings["orderNo"].Value = this.包装贴DTO.orderNo;
            btFormat.SubStrings["customerName"].Value = this.包装贴DTO.customerName;
            btFormat.SubStrings["materialNo"].Value = this.包装贴DTO.materialNo;
            btFormat.SubStrings["barcodeNo"].Value = this.包装贴DTO.barcodeNo;
            btFormat.SubStrings["shopName"].Value = this.包装贴DTO.shopName;

            
            Messages messages;
            int waitout = 10000; // 10秒 超时 
            Result nResult1 = btFormat.Print("套号吊牌" + this.包装贴DTO.barcode, waitout, out messages);
            btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession;
            //不保存对打开模板的修改 
            btFormat.Close(SaveOptions.DoNotSaveChanges);
            //结束打印引擎                  
            btEngine.Stop();
        }
    }

    public class 包装贴DTO
    {
        public String barcode { get; set; }
        public String businessNo { get; set; }
        public String orderNo { get; set; }
        public String customerName { get; set; }
        public String materialNo { get; set; }
        public String barcodeNo { get; set; }
        public String shopName { get; set; }
        public 包装贴DTO(string barcode, string businessNo, string orderNo, string customerName, string materialNo, string barcodeNo, string shopName)
        {
            this.barcode = barcode;
            this.businessNo = businessNo;
            this.orderNo = orderNo;
            this.customerName = customerName;
            this.materialNo = materialNo;
            this.barcodeNo = barcodeNo;
            this.shopName = shopName;
        }
        public 包装贴DTO()
        {
            this.barcode = "";
            this.businessNo = "";
            this.orderNo = "";
            this.customerName = "";
            this.materialNo = "";
            this.barcodeNo = "";
            this.shopName = "";
        }
    }
}
